//<script type='text/javascript'>

/**
 * 	csv upload extension (1 file at a time)
 *	communication with the media.php in request folder
 * 	communication with a json string in the moddump for setting up different types of csv upload
 */
 
var Csv = function ( vars ) {
	
	// build csv upload grid
	Object.extend(obj = new Element( "div", {id:"csv", class:"ext csv" }), dashboard.ext.csv);
	obj.insert( obj.grid = new Unit( "Csv upload." ));
		
	return obj.request();
}


dashboard.ext.csv	= {
	
	selected	: 0,
	
	// button to trigger the dashboard
	addbutton	: function() {
		obj = new Element("div", { id:"dashboard.ext.csvbutton", class:"ext_button csv", name:"ext:csv" }).observe('click', dashboard.showhide );
		obj.ext = Csv;
		return obj;
	},

	// request content for dashboard
	request		: function() {

		xhttp.send('control.call.php', { trg:"csv", type: "csv", action:"seed", vars:{ descr: 'csv upload.' }});			
		return this;
		
	},
	
	// seed the dashboard (from filter_seed in csv.php)
	seed	: function( response ) {
	
		// insert file upload
		response.each( function( e ) {
			this.down(0).insert( e );
		}, this );
		this.grid.enableScroll( 200 );
		
		// check every second for a file
		var instance = this;
		var interval = window.setInterval( function(){ instance.csv( interval )}, 1000 );
		
	},
	
	// check for csv
	csv 	: function( e ) {
	
		// check for uploaded files
		if( this.down(1).next(3).down('ul').childElements().first()) {
			
			// clear interval and set type 
			window.clearInterval( e );
			xhttp.send('control.call.php', { trg:"csv", type: "csv", action:"define", vars:{ descr: 'csv upload.' }});
			return this;	
		} 
	
	},
	
	// define type
	definer	: function( descr, content ) {
	
		// insert file upload
		content.each( function( e ) {
			this.down(0).insert( e );
		}, this );
		this.grid.enableScroll( 200 );
	
	},
	
	process	: function( response ) {
		
		
		if( answer = response.responseJSON ) {
			
			this.insert( this.info = new Info( "loading" ));

			// update h1
			this.info.down( 'h1' ).update( 'updated: ' + answer.type + ' (' + answer.count + ' rows)');
			
			// print columns
			/*answer.columns.each( function( el ) {
				this.info.down( '.content' ).down( 'ul' ).insert( new Element( "li" ).update( '<b>' + el + '</b>' ))
			}, this );
			
			answer.content.each( function( el ) {
				this.info.down( '.content' ).down( 'ul' ).insert( new Element( "li", { class: "clear" }));
				el.each( function( elem ) {
					this.info.down( '.content' ).down( 'ul' ).insert( new Element( "li" ).update( elem ))
				}, this );				
			}, this );
			
			this.info.enableScroll( 400 );*/
		}
	
	}
}
